
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.layout.mxCompactTreeLayout;
import com.mxgraph.layout.mxIGraphLayout;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.view.mxGraph;
import java.util.Map;
import javax.swing.JPanel;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author YixinGeng
 */

/*
 * The WholeTestPath class display the whole test path tree.
 *
 */
class WholeTestPath extends JPanel {

    public mxGraphComponent graphComponent;
    private mxGraph graph;
    public XMLImporter importer1;

    public WholeTestPath() {
        /*
         * import the whole path tree from the XML
         */
        importer1 = new XMLImporter();
        graph = new mxGraph();

        /*
         * displaying the path tree
         */
        Object parent = graph.getDefaultParent();

        // Sets the default vertex style
        Map<String, Object> style = graph.getStylesheet().getDefaultVertexStyle();
        style.put(mxConstants.STYLE_GRADIENTCOLOR, "#FFFFFF");
        style.put(mxConstants.STYLE_ROUNDED, true);
        style.put(mxConstants.STYLE_SHADOW, true);

        graph.getModel().beginUpdate();
        try {


            for (int i = 0; i < importer1.nodeList.size(); i++) {

                GraphNode node = importer1.nodeList.get(i);
                String tempId = Integer.toString(node.id);
                node.vertex = graph.insertVertex(parent, null, tempId,
                        0, 0, 30, 30,
                        "shape=ellipse;perimeter=ellipsePerimeter");


            }

            for (int i = 0; i < importer1.nodeList.size(); i++) {
                GraphNode pNode = importer1.nodeList.get(i);
                if (pNode.childNum > 0) {

                    if (pNode.childNum == 1) {
                        GraphNode sNode = importer1.nodeList.get(pNode.leftNodeId);
                        graph.insertEdge(parent, null, "", pNode.vertex, sNode.vertex);
                    }

                    if (pNode.childNum == 2) {
                        GraphNode sNodeLeft = importer1.nodeList.get(pNode.leftNodeId);
                        graph.insertEdge(parent, null, "", pNode.vertex, sNodeLeft.vertex);
                        GraphNode sNodeRight = importer1.nodeList.get(pNode.rightNodeId);
                        graph.insertEdge(parent, null, "", pNode.vertex, sNodeRight.vertex);
                    }
                }
            }

            /*
             * The current layout we use is the hierarchical layout. There are
             * also some others you can have a try such as the compact tree
             * layout and stack layout.
             */

            mxIGraphLayout layout = new mxHierarchicalLayout(graph);
//          mxIGraphLayout layout = new mxCompactTreeLayout(graph);
//          mxIGraphLayout layout = new mxStackLayout(graph);

            layout.execute(parent);

        } finally {
            graph.getModel().endUpdate();
        }

        this.pathDisplay();

        graph.getView().setScale(1);

        this.graphComponent = new mxGraphComponent(graph);
        this.add(this.graphComponent);
        this.setSize(800, 700);
        this.setVisible(true);
    }
    /*
     * the pathDisplay method is used to display the specific path on the graph.
     * Currently on the path, the shape of node will be changed from circle to
     * square.
     */

    private void pathDisplay() {
        Path path1 = new Path("0->1->2->3->4->17");
        Object[] pathNodeList = new Object[path1.pathLength];

        graph.getModel().beginUpdate();
        try {

            for (int i = 0; i < path1.pathLength; i++) {
                int index = path1.getPathNodes().get(i);
                pathNodeList[i] = importer1.nodeList.get(index).vertex;
            }
            String style = mxConstants.SHAPE_CLOUD;
            graph.setCellStyle(style, pathNodeList);
        } finally {
            graph.getModel().endUpdate();
        }



    }
}
